/*
 * Copyright 2008 Les Hazlewood
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.pojodm.party;

import org.pojodm.entity.Entity;
import org.pojodm.entity.Referenceable;

/**
 * Created on: Nov 2, 2005 2:47:13 PM
 *
 * @author Les Hazlewood
 */
public class Organization extends Party implements Referenceable, Comparable<Organization> {

    private String customId;

    private String name;

    private String shortName;

    public Organization(){}

    public Organization( String name, String customId ) {
        setName( name );
        setCustomId( customId );
    }

    public Organization( String customId, String name, String shortName ) {
        setCustomId( customId );
        setName( name );
        setShortName( shortName );
    }

    public String getCustomId() {
        return customId;
    }

    public void setCustomId( String customId ) {
        this.customId = customId;
    }

    public String getName() {
        return name;
    }

    public void setName( String name ) {
        this.name = name;
    }

    public String getShortName() {
        return shortName;
    }

    public void setShortName(String shortName) {
        this.shortName = shortName;
    }

    public int compareTo(Organization organization) {
        if ( organization == null ) {
            return 1;
        } else {
            String name = getName();
            String otherName = organization.getName();
            if ( name != null ) {
                if ( otherName == null ) {
                    return 1;
                } else {
                    return name.compareTo(organization.getName());
                }
            } else {
                if ( otherName != null ) {
                    return -1;
                } else {
                    return 0;
                }
            }
        }
    }

    public boolean onEquals( Entity e ) {
        if ( e instanceof Organization ) {
            Organization org = (Organization)e;
            return getCustomId().equals( org.getCustomId() );
        }
        return false;
    }

    public StringBuffer toStringBuffer() {
        StringBuffer sb = super.toStringBuffer();
        sb.append(",name=").append( getName() );
        sb.append(",customId=[").append( getCustomId() ).append( "]" );
        return sb;
    }

    public int hashCode() {
        return getCustomId().hashCode();
    }

    /**
     * Returns a shallow copy - the existing parent and children collection object references are
     * just passed in (set) to the cloned object.
     * @return an Organization clone
     */
    @Override
    @SuppressWarnings( { "CloneDoesntDeclareCloneNotSupportedException" } )
    public Object clone() {
        Organization clone = (Organization)super.clone();
        clone.setName( getName() );
        clone.setCustomId( getCustomId() );
        return clone;
    }

    public String getLabel() {
        return getName() + " *";
    }

    public void accept(PartyVisitor visitor) {
        visitor.visit( this );
    }
}



